﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class WebUserControl_ControlThongTinKhachHang : System.Web.UI.UserControl
{
   
    string error = "";
  
    ClassExecute.ClassExecute dt;
    string str = BCDataCenter.Configuration.CurrentConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            HienThiTinh();
            HienThiQuan(DropTinh.SelectedValue.ToString());
        }
    }
    //void AddCustomerKey()
    //{
    //    SqlConnection con = new SqlConnection(BCDataCenter.Configuration.CurrentConnectionString); ;
    //    common = new MethodClassCommon();
    //    string cust = "1";
    //    try
    //    {
    //        con.Open();
    //        SqlCommand c = new SqlCommand("GET_LAST_CUSTOMERKEY", con);
    //        c.CommandType = CommandType.StoredProcedure;
    //        cust = c.ExecuteScalar().ToString();
    //        CustomerKey = new SqlParameter("@CustomerKey", Convert.ToString((float.Parse(cust) + 1)));

    //    }
    //    catch
    //    {
    //        CustomerKey = new SqlParameter("@CustomerKey", cust);
    //    }
    //    finally
    //    {
    //        con.Close();
    //    }
        
    //}
    //void AddParameter()
    //{ 
    //    CustomerName=new SqlParameter("@CustomerName",txtTenKhachHang.Text);
    //    IDNumber=new SqlParameter("@IDNumber",txtSoCMND.Text);
    //    BirthDay = new SqlParameter("@BirthDay", txtNgaySinh.Text);
    //    if (CheckGioiTinh.Checked)
    //    {
    //        Sex = new SqlParameter("@Sex", "True");
    //    }
    //    else
    //    {
    //        Sex = new SqlParameter("@Sex", "False");
    //    }
    //    Address = new SqlParameter("@Address", txtDiaChi.Text);
    //    Mobile = new SqlParameter("@Mobile", txtDTNha.Text);
    //    Tell = new SqlParameter("@Tell", txtDiDong.Text);
    //    ComPhone = new SqlParameter("@ComPhone", txtDTCoQuan.Text);
    //    if (CheckHonNhan.Checked)
    //    {
    //        MarStatus = new SqlParameter("@MarStatus", "True");
    //    }
    //    else
    //    {
    //        MarStatus = new SqlParameter("@MarStatus", "False");
    //    }
    //    Job = new SqlParameter("@Job", txtNgheNghiep.Text);
    //    SkinType = new SqlParameter("@SkinType", txtLoaiDa.Text);
    //    SkinBeforeUsed = new SqlParameter("@SkinBeforeUsed", txtSkinBefore.Text);
    //    salesp_key = new SqlParameter("@salesp_key", txtMaTVV.Text);
    //    Sitecode = new SqlParameter("@Sitecode","khoi");
    //    LastBuy = new SqlParameter("@LastBuy",System.DateTime.Now.Date.ToString("dd/MM/yyyy"));
    //}

    protected void DropTinh_SelectedIndexChanged1(object sender, EventArgs e)
    {
        HienThiQuan(DropTinh.SelectedValue.ToString());
    }
    void HienThiQuan(string DropTinh)
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "GET_DATA_DISTRICTOWN_PROVINCECODE", CommandType.StoredProcedure,
            new SqlParameter("@ProvinceCode", DropTinh));
        DropQuanHuyen.DataSource = db;
        DropQuanHuyen.DataTextField = "TownName";
        DropQuanHuyen.DataValueField = "TownCode";
        DropQuanHuyen.DataBind();
    }
    void HienThiTinh()
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "GET_DATA_PROVINCE", CommandType.StoredProcedure);
        DropTinh.DataSource = db;
        DropTinh.DataTextField = "ProvinceName";
        DropTinh.DataValueField = "ProvinceCode";
        DropTinh.DataBind();
    }
   
    protected void imgTaoMoi_Click(object sender, ImageClickEventArgs e)
    {
        txtMaKH.Text = "auto";
        txtTenKhachHang.Text = "";
        txtBeforeSkin.Text = "";
        txtDiaChi.Text = "";
        txtDiDong.Text = "";
        txtDTNha.Text = "";
        txtNgaySinh.Text = "";
        txtNgheNghiep.Text = "";
        txtSkin.Text = "";
        txtSoCMND.Text = "";
        txtVip.Text = "";
        ViewState["flag"] = true;
    }
   
    protected void imgSave_Click(object sender, ImageClickEventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        if ((bool)ViewState["flag"])
        {
            string kq = dt.GetDataDepend(str, "ls_updateCustomerInfo", CommandType.StoredProcedure, ref error,
                new SqlParameter("@customer_key", "auto"),
                new SqlParameter("@customer_name", txtTenKhachHang.Text),
                new SqlParameter("@address", txtDiaChi.Text),
                new SqlParameter("@phone", txtDiDong.Text),
                new SqlParameter("@ProvinceCode", DropTinh.SelectedValue.ToString()),
                new SqlParameter("@TownCode", DropQuanHuyen.SelectedValue.ToString()),
                new SqlParameter("@skintype", txtSkin.Text),
                new SqlParameter("@BSkinType", txtBeforeSkin.Text),
                new SqlParameter("@sex", CheckGioiTinh.Checked.ToString()),
                new SqlParameter("@Birthday", txtNgaySinh.Text),
                new SqlParameter("@IDCardNo", txtSoCMND.Text),
                new SqlParameter("@HomePhoneNo", txtDTNha.Text),
                new SqlParameter("@isSingle", CheckHonNhan.Checked.ToString()),
                new SqlParameter("@Career", txtNgheNghiep.Text),
                new SqlParameter("@Recuser", Session["username"].ToString()),
                new SqlParameter("@siteCode", Session["sitecode"].ToString()));
            uscMsgBox1.AddMessage("Đăng ký khách hàng thành công", uscMsgBox.enmMessageType.Success);
            txtMaKH.Text = kq;
            ViewState["flag"] = false;
        }
        else
        {
            string kq = dt.GetDataDepend(str, "ls_updateCustomerInfo", CommandType.StoredProcedure, ref error,
                new SqlParameter("@customer_key", txtMaKH.Text),
                new SqlParameter("@customer_name", txtTenKhachHang.Text),
                new SqlParameter("@address", txtDiaChi.Text),
                new SqlParameter("@phone", txtDiDong.Text),
                new SqlParameter("@ProvinceCode", DropTinh.SelectedValue.ToString()),
                new SqlParameter("@TownCode", DropQuanHuyen.SelectedValue.ToString()),
                new SqlParameter("@skintype", txtSkin.Text),
                new SqlParameter("@BSkinType", txtBeforeSkin.Text),
                new SqlParameter("@sex", CheckGioiTinh.Checked.ToString()),
                new SqlParameter("@Birthday", txtNgaySinh.Text),
                new SqlParameter("@IDCardNo", txtSoCMND.Text),
                new SqlParameter("@HomePhoneNo", txtDTNha.Text),
                new SqlParameter("@isSingle", CheckHonNhan.Checked.ToString()),
                new SqlParameter("@Career", txtNgheNghiep.Text),
                new SqlParameter("@Recuser",Session["username"].ToString()),
                new SqlParameter("@siteCode", Session["sitecode"].ToString()));
            uscMsgBox1.AddMessage("Cập nhật khách hàng thành công", uscMsgBox.enmMessageType.Success);
           
        }
    }
   
    protected void imgTim_Click(object sender, ImageClickEventArgs e)
    {
        TabContainer1.ActiveTabIndex = 1;
       
    }
  
    void getKhachHang()
    {

        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "ls_searchCustomerInfo", CommandType.StoredProcedure,
            new SqlParameter("@siteCode",Session["sitecode"].ToString()),
            new SqlParameter("@parameter",TextBox1.Text));
       if (db.Rows.Count > 0)
        {
            GridView1.DataSource = db;
            GridView1.DataBind();
        }
       
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        
        getKhachHang();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        getKhachHang();
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "chon")
        {
            dt = new ClassExecute.ClassExecute();
            GridViewRow grv = ((Control)e.CommandSource).NamingContainer as GridViewRow;
            DataTable db = dt.Get_Data_DataTable(str, ref error, "ls_getCustomerInfo", CommandType.StoredProcedure,
                new SqlParameter("@CustomerKey", grv.Cells[0].Text));
            if (db.Rows.Count > 0)
            {
                txtMaKH.Text = db.Rows[0]["customer_key"].ToString();
                txtTenKhachHang.Text = db.Rows[0]["customer_name"].ToString();
                txtSoCMND.Text = db.Rows[0]["IDCardNo"].ToString();
                txtNgaySinh.Text = db.Rows[0]["Birthday"].ToString();
                CheckGioiTinh.Checked = bool.Parse(db.Rows[0]["sex"].ToString());
                CheckHonNhan.Checked = bool.Parse(db.Rows[0]["isSingle"].ToString());
                txtDiaChi.Text = db.Rows[0]["address"].ToString();
                try
                {
                    DropTinh.SelectedValue = db.Rows[0]["ProvinceCode"].ToString();
                    HienThiQuan(DropTinh.SelectedValue.ToString());
                }
                catch
                {

                }
                try
                {
                    DropQuanHuyen.SelectedValue = db.Rows[0]["TownCode"].ToString();
                }
                catch
                {

                }
                txtDiDong.Text = db.Rows[0]["phone"].ToString();
                txtDTNha.Text = db.Rows[0]["HomePhoneNo"].ToString();
                txtNgheNghiep.Text = db.Rows[0]["Career"].ToString();
                txtSkin.Text = db.Rows[0]["skintype"].ToString();
                txtBeforeSkin.Text = db.Rows[0]["BSkinType"].ToString();
                txtVip.Text = db.Rows[0]["VIPCardNo"].ToString();
                ViewState["flag"] = false;
                TabContainer1.ActiveTabIndex = 0;
            }
        }
    }
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        getKhachHang();
    }
    protected void ImageMuaHang_Click(object sender, ImageClickEventArgs e)
    {
        if ((txtMaKH.Text != "auto") && (txtMaKH.Text!="") && (txtMaKH.Text!=null)) 
        {
            Response.Redirect(BCDataCenter.Globals.ApplicationPath + "/WebMaster/Administrator/NhapHoaDonBHShowRoom/Default.aspx?customerkey=" + txtMaKH.Text);
        }
    }
}

